package com.qijin.util;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qijin.config.MpayConstant;
import com.qijin.dto.EncryptRequestDto;
import com.qijin.dto.base.BaseRequestDto;
import com.qijin.dto.business.BankcardBinRequestDto;
import com.qijin.dto.business.BankcardBindRequestDto;
import com.qijin.dto.business.BankcardSignCreateBillDto;
import com.qijin.util.lianpay.LianLianPaySecurity;
import com.qijin.vo.BankcardBindResponseVo;
import com.qijin.vo.BankcardSignCreateVo;
import com.qijin.vo.BaseResponseVo;
import com.qijin.vo.BaseVo;
import lombok.extern.slf4j.Slf4j;
import lombok.var;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.qijin.util.HttpUtil.doPost;
import static com.qijin.util.HttpUtil.genSignData;

@Slf4j
public class MPayUtil {


    /**
     * lianlian支付静态方法
     */
    public static BaseResponseVo exec(BaseRequestDto dto) throws Exception {
        String url = dto.getUrl();
        String voPath = dto.getVoPath();
        dto.setUrl(null);
        dto.setVoPath(null);
        dto.setSign(GenSignUtils.genSign(JSONObject.parseObject(JSONObject.toJSONString(dto)), "", MpayConstant.priKey));
        String res;
        log.info("----------------------------连连支付请求参数：" + JSONObject.toJSONString(dto));
        if (dto instanceof EncryptRequestDto) {
            String encryptStr = LianLianPaySecurity.encrypt(JSONObject.toJSONString(dto), MpayConstant.pubKey);
            JSONObject json = new JSONObject();
            json.put("oid_partner", MpayConstant.merchantId);
            json.put("pay_load", encryptStr);
            res = doPost(url, json, "UTF-8");
            log.info("----------------------------连连支付返回参数：" + res);
            JSONObject jsonRs = JSONObject.parseObject(res);
            boolean checkSign = SHA256withRSAUtil.checksign(MpayConstant.pubKey, genSignData(jsonRs), jsonRs.getString("sign"));
            if (!checkSign) {
                throw new Exception("验签失败！");
            }
        } else {
            res = doPost(url, JSONObject.toJSONString(dto), "UTF-8");
            log.info("----------------------------连连支付返回参数：" + res);
        }
        BaseResponseVo baseResponseVo = JSON.parseObject(res, BaseResponseVo.class);


        BaseResponseVo bv;
        if (baseResponseVo.getRet_code().equals("8888") || baseResponseVo.getRet_code().equals("0000")) {
            Class<?> aClass = Class.forName(voPath);
            bv = (BaseResponseVo) JSON.parseObject(res, aClass);
        } else {
            bv = JSON.parseObject(res, BaseVo.class);
        }
        System.out.println(bv);
        return bv;
    }


    public static void main(String[] args) throws Exception {
//        BankcardSignCreateBillDto bankcardSignCreateBillDto = new BankcardSignCreateBillDto();
//        bankcardSignCreateBillDto.setId_no("130131198206281510");
//        bankcardSignCreateBillDto.setAcct_name("郭刚");
//        bankcardSignCreateBillDto.setCard_no("6228480639312449072");
//        bankcardSignCreateBillDto.setDt_order(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
//        bankcardSignCreateBillDto.setNo_order(String.valueOf(IdWorker.getId()));
//        bankcardSignCreateBillDto.setUser_id("5332");
//        Map<String, String> item = new HashMap<>();
//        item.put("user_info_full_name", "郭刚");
//        item.put("user_info_id_type", "0");
//        item.put("user_info_id_no", "130131198206281510");
//        item.put("user_info_identify_state", "1");
//        item.put("user_info_identify_type", "1");
//
//        item.put("frms_ware_category", "1");
//        item.put("user_info_mercht_userno", "5332");
//        item.put("user_info_bind_phone", "13691233065");
//        item.put("user_info_dt_register", DateUtil.format(new Date(), "YYYYMMDDHHMMSS"));
//        item.put("goods_name", "保理款");
//
//
//
//        bankcardSignCreateBillDto.setRisk_item(JSON.toJSONString(item));
//
//        System.out.println(JSON.toJSONString(bankcardSignCreateBillDto));
//        BaseResponseVo exec = exec(bankcardSignCreateBillDto);
//        System.out.println(JSON.toJSONString(exec));
//        BankcardSignCreateVo b = (BankcardSignCreateVo) exec;
//        System.out.println(b);


        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String nowStr = df.format(date);

        //绑卡
        var bean = new BankcardBindRequestDto();
        bean.setAcct_name("刘甜");
        bean.setCard_no("6222080402008410488");
        bean.setBind_mob("15830168189");
        bean.setBusi_partner("101001");
        bean.setDt_order(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        bean.setNo_order(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        bean.setId_type("0");
        bean.setId_no("130104198810041530");
        bean.setUser_id("4127251931021925");
        BaseResponseVo e1 = exec(bean);
        BankcardBindResponseVo bankcardBindResponseVo;
        if (e1 instanceof BankcardBindResponseVo) {
            bankcardBindResponseVo = (BankcardBindResponseVo) e1;
        }
//
//        //短信验证
//        var bankcardBindVerfyRequestDto = new BankcardBindVerfyRequestDto();
//        bankcardBindVerfyRequestDto.setToken(bankcardBindResponseVo.getToken());
//        bankcardBindVerfyRequestDto.setVerify_code("845214");
//        bankcardBindVerfyRequestDto.setUser_id(bankcardBindResponseVo.getUser_id());
//        BankcardBindVerfyResponseVo bankcardBindVerfyResponseVo;
//        BaseResponseVo e2 = exec(bankcardBindVerfyRequestDto);
//        if (e2 instanceof BankcardBindVerfyResponseVo) {
//            bankcardBindVerfyResponseVo = (BankcardBindVerfyResponseVo) e2;
//        } else {
//            return;
//        }
//
////        //签约支付
//        BankcardPrePayRequestDto bankcardPrePayRequestDto = new BankcardPrePayRequestDto();
//        bankcardPrePayRequestDto.setAcct_name("刘甜");
//        bankcardPrePayRequestDto.setNo_agree(bankcardBindVerfyResponseVo.getNo_agree());
//        bankcardPrePayRequestDto.setDt_order(nowStr);
//        bankcardPrePayRequestDto.setNo_order(nowStr);
//        bankcardPrePayRequestDto.setUser_id(bankcardBindVerfyResponseVo.getUser_id());
//        bankcardPrePayRequestDto.setInfo_order("mpayapi-test");
//        bankcardPrePayRequestDto.setMoney_order("1.00");
//        bankcardPrePayRequestDto.setName_goods("商品名称");
//        BankcardPrePayResponseVo bankcardPrePayResponseVo;
//        BaseResponseVo e3 = exec(bankcardPrePayRequestDto);
//        if (e3 instanceof BankcardPrePayResponseVo) {
//            bankcardPrePayResponseVo = (BankcardPrePayResponseVo) e3;
//        } else {
//            return;
//        }
//        BankcardBinRequestDto b = new BankcardBinRequestDto();
//        b.setCard_no("6228480639312449072");
//        BaseResponseVo exec = exec(b);

    }
}
